from PyQt4.QtGui import *
from PyQt4.QtCore import *

from EntityFactory import EntityFactory

class TabEntities(QWidget):

	TYPE_MOB   = 1
	TYPE_DECOR = 2
	TYPE_ITEM  = 3

	def __init__(self):
		QWidget.__init__(self)

		layout = QVBoxLayout()

		self.tree = QTreeWidget()
		self.tree.setHeaderLabels(["ID", "Name", "HP", "Speed", "preview"])
		self.tree.setSortingEnabled(True)
		self.tree.header().setResizeMode(QHeaderView.Stretch)

		layout.addWidget(self.tree)

		self.setLayout(layout)

		mobs = QTreeWidgetItem(self.tree)
		mobs.setText(0, "Mobs")

		e = EntityFactory()
		# feed tree with mobs
		for mob_id, mob in e.mobs.iteritems():
			item = QTreeWidgetItem(mobs, self.TYPE_MOB)
			item.setText(0, mob_id)
			item.setText(1, mob.name)
			item.setText(2, str(mob.hp))
			item.setText(3, str(mob.speed))
			item.setData(4, Qt.DecorationRole, mob.image.scaledToHeight(24, Qt.SmoothTransformation))

		decors = QTreeWidgetItem(self.tree)
		decors.setText(0, "Decors")
		self.tree.addTopLevelItem(decors)

		items = QTreeWidgetItem(self.tree)
		items.setText(0, "Items")
		self.tree.addTopLevelItem(items)

		self.tree.currentItemChanged.connect(self.onItemSelected)

	def onItemSelected(self, item):
		if item.type() == self.TYPE_MOB:
			self.emit(SIGNAL("mob_selected"), item.text(0))
		elif item.type() == self.TYPE_DECOR:
			self.emit(SIGNAL("decor_selected"), item.text(0))
		elif item.type() == self.TYPE_ITEM:
			self.emit(SIGNAL("item_selected"), item.text(0))
